<?php
/*
Author: Hallvard Westman
Project: Links
 ---------------------------------USER PAGE FOR LINKS----------------------------------------*/

session_start();

include 'db.php';
include 'userclass.php';


/*---------------------------------INSERTING LINK----------------------------------------*/	
		
		$UserId=$_GET['id'];
		
		if (isset ($_POST['Url'])) {
		$link=$_POST['Url'];
		if(substr($link, 0, 4)!="http") 
			$link = "http://".$link;
		
		$sql = 'INSERT INTO li_links (UserId,Url)
					VALUES(:UserId, :Url)';
			
			$sth = $db->prepare($sql);
			$sth->bindParam(':UserId',$UserId);
			$sth->bindParam(':Url',$link);
			$res = $sth->execute();
			if(!$res){
			echo "<a>Error while inserting</a>";
			header('location: archive.php?id='.$UserId);
			}
			header('location: archive.php?id='.$UserId);
		}	
		
		
		
		
/*---------------------------------DISPLAYS LINKS----------------------------------------*/
		
		include 'header.php';
		include 'bar.php';
		
		$sql = 'SELECT Url FROM li_links WHERE UserId =:UserId ORDER BY Count DESC';
		$sth = $db->prepare($sql);
		$sth->bindParam (':UserId', $UserId);
		$sth->execute();
		//for ($i=1;$i<=8;$i++){
		//$row = $sth->fetch();
		?><div id="main-content"> <?php
		while($row = $sth->fetch()){
		//If links does not contain http (lets it redirect)
		$url=$row['Url'];
		?>	
		<a href="link.php?link=<?php echo $url; ?>"><?php echo $url; ?></a><br/>
		
		<?php   }   
/*---------------------------------FORM FOR LINK INSERT----------------------------------------*/
		?></div>
		
		<div id="url">
		<form method="post" action="" enctype="multipart/form-data">
		<ul>
		<!--<li><label for="Url">URL</label></li>-->
		<li><input type="text" name="Url" value="Url"/></li>
		<li><button type="submit">Add</button></li>
		
		</ul>
		
		</form>
		</div>	
<?php
include 'footer.php';

